package com.gemcoder.system.service;
import static com.gemcoder.system.datasource.JdbcTemplateUtil.executeSQL;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import static com.gemcoder.system.service.UserSdkService.sdkOfGetCurrentUserName;
import static com.gemcoder.system.service.UserSdkService.sdkOfGetCurrentUserId;
import com.gemcoder.system.bo.Result;
import static com.gemcoder.system.service.permission.PermissionSdkService.sdkOfgetAllPermissions;
import static com.gemcoder.system.service.permission.PermissionSdkService.sdkOfgetAllMenus;
import java.sql.SQLException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import cn.hutool.core.util.StrUtil;

import cn.hutool.core.util.StrUtil;

@Service
public class ScoringmanagementService {

    public Result getRatingManagementData(Map<String,Object> paramMap){
    // 获取分页参数
    Integer current = paramMap.get("current") != null ? Integer.valueOf(String.valueOf(paramMap.get("current"))) : 1;
    Integer pageSize = paramMap.get("pageSize") != null ? Integer.valueOf(String.valueOf(paramMap.get("pageSize"))) : 10;
    String projectName = paramMap.get("projectName") != null ? String.valueOf(paramMap.get("projectName")) : null;

    // 构建查询SQL
    StringBuilder sql = new StringBuilder("SELECT id, project_name, project_code, score FROM score_management WHERE 1=1");
    List<Object> params = new ArrayList<>();
    if (projectName != null && !projectName.isEmpty()) {
        sql.append(" AND project_name LIKE ?");
        params.add("%" + projectName + "%");
    }
    sql.append(" LIMIT ?, ?");
    params.add((current - 1) * pageSize);
    params.add(pageSize);

    // 执行查询SQL
    List<Map<String, Object>> data = executeSQL(sql.toString(), params.toArray());

    // 构建统计总数SQL
    StringBuilder countSql = new StringBuilder("SELECT COUNT(*) as total FROM score_management WHERE 1=1");
    List<Object> countParams = new ArrayList<>();
    if (projectName != null && !projectName.isEmpty()) {
        countSql.append(" AND project_name LIKE ?");
        countParams.add("%" + projectName + "%");
    }
    List<Map<String, Object>> countResult = executeSQL(countSql.toString(), countParams.toArray());
    Integer total = countResult.get(0) != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;

    // 返回结果
    Result result = new Result();
    result.setFlag(true);
    result.setMessage("查询成功");
    Map<String, Object> resultData = new HashMap<>();
    resultData.put("total", total);
    resultData.put("data", data);
    result.setData(resultData);
    return result;
}

public Result addRatingManagementData(Map<String,Object> paramMap){
    Object scorevalue1 = paramMap.get("Ratingvalue1");
    double score;
    try {
        score = Double.parseDouble(String.valueOf(scorevalue1));
    } catch (Exception e) {
        return new Result(false, "评分值必须为数字");
    }

    // 获取表单参数
    String projectName = paramMap.get("ProjectNamevalue1") != null ? String.valueOf(paramMap.get("ProjectNamevalue1")) : null;
    String projectCode = paramMap.get("ItemNovalue1") != null ? String.valueOf(paramMap.get("ItemNovalue1")) : null;
    String createUsername = sdkOfGetCurrentUserName();

    // 构建插入SQL
    StringBuilder sql = new StringBuilder("INSERT INTO score_management (project_name, project_code, score, create_username, create_time) VALUES (?, ?, ?, ?, NOW())");
    List<Object> params = new ArrayList<>();
    params.add(projectName);
    params.add(projectCode);
    params.add(score);
    params.add(createUsername);

    // 执行插入SQL
    int insertRows = executeSQL(sql.toString(), params.toArray());

    // 返回结果
    Result result = new Result();
    if (insertRows > 0) {
        result.setFlag(true);
        result.setMessage("新增成功");
    } else {
        result.setFlag(false);
        result.setMessage("新增失败");
    }
    return result;
}

    public Result updateRatingManagementData(Map<String,Object> paramMap) {
        // 获取表单参数
        Integer id = paramMap.get("id") != null ? Integer.valueOf(String.valueOf(paramMap.get("id"))) : null;
        String projectName = paramMap.get("ProjectNamevalue2") != null ? String.valueOf(paramMap.get("ProjectNamevalue2")) : null;
        String projectCode = paramMap.get("ItemNovalue2") != null ? String.valueOf(paramMap.get("ItemNovalue2")) : null;
        Object scorevalue2 = paramMap.get("Ratingvalue2");
        double score;

        // 验证评分值
        try {
            score = Double.parseDouble(String.valueOf(scorevalue2));
        } catch (Exception e) {
            return new Result(false, "评分值必须为数字");
        }

        // 构建更新SQL
        StringBuilder sql = new StringBuilder("UPDATE score_management SET project_name = ?, project_code = ?, score = ?, update_time = NOW() WHERE id = ?");
        List<Object> params = new ArrayList<>();
        params.add(projectName);
        params.add(projectCode);
        params.add(score);
        params.add(id);

        // 执行更新SQL
        int updateRows = executeSQL(sql.toString(), params.toArray());

        // 返回结果
        Result result = new Result();
        if (updateRows > 0) {
            result.setFlag(true);
            result.setMessage("更新成功");
        } else {
            result.setFlag(false);
            result.setMessage("更新失败，可能是ID不存在");
        }
        return result;
    }


public Result deleteRatingManagementData(Map<String,Object> paramMap){
    // 获取删除参数
    Integer id = paramMap.get("id") != null ? Integer.valueOf(String.valueOf(paramMap.get("id"))) : null;

    // 构建删除SQL
    StringBuilder sql = new StringBuilder("DELETE FROM score_management WHERE id = ?");
    List<Object> params = new ArrayList<>();
    params.add(id);

    // 执行删除SQL
    int deleteRows = executeSQL(sql.toString(), params.toArray());

    // 返回结果
    Result result = new Result();
    if (deleteRows > 0) {
        result.setFlag(true);
        result.setMessage("删除成功");
    } else {
        result.setFlag(false);
        result.setMessage("删除失败");
    }
    return result;
}

}